﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MicroMvvm;
using GestionDeCinebox.Classes;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Data;

namespace GestionDeCinebox.ViewModel
{
    public class FilmViewModel : ObservableObject
    {
        private ObservableCollection<Film> listFilm = new ObservableCollection<Film>();
        private DateTime choixDate;

        public DateTime ChoixDate
        {
            get { return choixDate; }
            set 
            { 
                choixDate = value;
                RaisePropertyChanged("ChoixDate");
            }
        }
        public ObservableCollection<Film> ListFilm
        {
            get { return listFilm; }
            set 
            { 
                listFilm = value;
                RaisePropertyChanged("ListFilm");
            }
        }

        public ICommand ChercherFilm
        {
            get { return new RelayCommand(DoChercherFilm); }
        }

        public ICommand AllFilm
        {
            get { return new RelayCommand(DonnerTousFilms); }
        }

        public ICommand FilmEnCours
        {
            get { return new RelayCommand(DonnerFilmEnCours); }
        }

        public ICommand FilmAVenir
        {
            get { return new RelayCommand(DonnerFilmAVenir); }
        }

        public ICommand FilmDejaFilmer
        {
            get { return new RelayCommand(DonnerFilmDejaFilmer); }
        }

        void DoChercherFilm()
        {
            List<SqlParameter> lstPara = new List<SqlParameter>();
            //para 1
            lstPara.Add(new SqlParameter("@ngayGioChieu", choixDate));
            //para 2
            int type = 2;
            if (choixDate.Hour == 0 && choixDate.Year == DateTime.Now.Year)
            {
                type = 0;
            }
            else if(choixDate.Year == 1 && choixDate.Hour != 0)
            {
                type = 1;
            }
            else
            {
                type = 2;
            }
            lstPara.Add(new SqlParameter("@type", type));
            //load list
            LoadListFilm("dbo.sp_LayPhimTheoNgayChieu", lstPara);
        }

        void DonnerTousFilms()
        {
            LoadListFilm("dbo.sp_TatCaPhim", new List<SqlParameter>());
        }

        void DonnerFilmDejaFilmer()
        {
            LoadListFilm("dbo.sp_LayPhimDaChieu", new List<SqlParameter>());
        }

        void DonnerFilmAVenir()
        {
            LoadListFilm("sp_LayPhimSapChieu", new List<SqlParameter>());
        }

        void DonnerFilmEnCours()
        {
            if (ListFilm.Count > 0)
            {
                ListFilm.Clear();
            }
        }

        void LoadListFilm(String nameStore, List<SqlParameter> para)
        {
            if (ListFilm.Count > 0)
            {
                ListFilm.Clear();
            }
            using (SqlCommand command = new SqlCommand(nameStore))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = DataProvider.Con;
                for (int i = 0; i < para.Count; i++ )
                {
                    command.Parameters.Add(para[i]);
                }

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Film f = new Film();
                        f.IdFilm = reader.GetString(reader.GetOrdinal("IDFilm"));
                        f.NomF = reader.GetString(reader.GetOrdinal("NomF"));
                        f.Author = reader.GetString(reader.GetOrdinal("Author"));
                        f.Acteurs = reader.GetString(reader.GetOrdinal("Acteurs"));
                        f.Info = reader.GetString(reader.GetOrdinal("Info"));
                        int duree = reader.GetInt32(reader.GetOrdinal("Duree"));
                        f.Duree = duree.ToString() + " minutes";
                        f.Lang = reader.GetString(reader.GetOrdinal("Language"));
                        f.TypeF = reader.GetString(reader.GetOrdinal("TypeF"));
                        f.Img = (new ImageProcessor()).LoadImage(f.IdFilm);
                        ListFilm.Add(f);
                    }
                }
            }
        }
    }
}
